/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：统一重新命名，可以修改场景对象名或项目资源名
* CreateTime：2020-09-24 15:54:13
* Version：1.0
* Modify Recorder：
*************************************************/

using TinyToolKit;
using UnityEditor;
using UnityEngine;

namespace TinyToolKitEditor
{
    public class UnifyRename : EditorWindow
    {
        private static UnifyRename _instance;

        private string prefix;
        private string joint = "_"; 
        private int startIndex = 1;

        public static void ShowWindow()
        {
            _instance = GetWindow<UnifyRename>();
            _instance.titleContent = new GUIContent("Unify Rename");
            _instance.maxSize = new Vector2(250,120);
            _instance.minSize = new Vector2(250,120);
            _instance.Show();
        }

        private void OnGUI()
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("构成格式：前缀+连接符号+起始序列");
            EditorGUILayout.LabelField("示例：Cube_1");
            EditorGUILayout.Space();
            
            EditorGUILayout.BeginHorizontal();
            prefix = EditorGUILayout.TextField(prefix,GUILayout.Width(120));
            EditorGUILayout.LabelField("+",GUILayout.Width(14));
            joint = EditorGUILayout.TextField(joint,GUILayout.Width(12));
            EditorGUILayout.LabelField("+",GUILayout.Width(14));
            startIndex = EditorGUILayout.IntField(startIndex,GUILayout.Width(60));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.Space();
            EditorGUI.BeginDisabledGroup(string.IsNullOrEmpty(prefix) || Selection.objects.Length == 0);
            if (GUILayout.Button("确定"))
            {
                if (Selection.objects.Length > 0)
                {
                    //命名为临时文件名，防治重名导致改名失败
                    for (int i = 0; i < Selection.objects.Length; i++)
                    {
                        string newName = "TempName" + joint + (i + startIndex); 
                       
                        //场景对象修改命名
                        if (Selection.objects[i] is GameObject)
                            Selection.objects[i].name = newName;
                        else//项目资源修改命名
                        {
                            string assetPath = AssetDatabase.GetAssetPath(Selection.objects[i]);
                            if (assetPath.IsNullOrEmpty() == false)
                                AssetDatabase.RenameAsset(assetPath, newName);
                        }
                    }
                    
                    for (int i = 0; i < Selection.objects.Length; i++)
                    {
                        string newName = prefix + joint + (i + startIndex);
                       
                        //场景对象修改命名
                        if (Selection.objects[i] is GameObject)
                            Selection.objects[i].name = newName;
                        else//项目资源修改命名
                        {
                            string assetPath = AssetDatabase.GetAssetPath(Selection.objects[i]);
                            if (assetPath.IsNullOrEmpty() == false)
                                AssetDatabase.RenameAsset(assetPath, newName); 
                        }
                        EditorUtility.SetDirty(Selection.objects[i]);
                    }
                    
                    AssetDatabase.SaveAssets();
                    AssetDatabase.Refresh();
                    
                    Close();
                }
            }
            EditorGUI.EndDisabledGroup();
            EditorGUILayout.LabelField("当前修改个数：" + Selection.objects.Length);
            
            Repaint();
        }
    }
}

